const pdf = ['pdf'];
const doc = ['doc', 'docx'];
const xls = ['xls', 'xlsx', 'csv'];
const ppt = ['ppt', 'pptx'];

const extTypeMap = {
    // zip
    '7z': 'application/x-7z-compressed',
    rar: 'application/vnd.rar',
    tar: 'application/x-tar',
    zip: 'application/zip',
    gz: 'application/gzip',
    jar: 'application/java-archive',
    bz: 'application/x-bzip',
    bz2: 'application/x-bzip2',

    // img
    ics: 'text/calendar',
    ico: 'image/vnd.microsoft.icon',
    gif: 'image/gif',
    png: 'image/png',
    jpg: 'image/jpeg',
    jpeg: 'image/jpeg',
    bmp: 'image/bmp',
    avif: 'image/avif',
    svg: 'image/svg+xml',
    tiff: 'image/tiff',
    webp: 'image/webp',

    // office
    pdf: 'application/pdf',
    doc: 'application/msword',
    docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    ppt: 'application/vnd.ms-powerpoint',
    pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    xls: 'application/vnd.ms-excel',
    xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    csv: 'text/csv',

    // audio
    mp3: 'audio/mpeg',
    aac: 'audio/aac',
    wav: 'audio/wav',
    weba: 'audio/webm',
    'audio-3gp': 'audio/3gpp',
    oga: 'audio/ogg',

    // video
    mp4: 'video/mp4',
    mpeg: 'video/mpeg',
    avi: 'video/x-msvideo',
    ts: 'video/mp2t',
    webm: 'video/webm',
    '3gp': 'video/3gpp',
    ogv: 'video/ogg',

    //  txt
    txt: 'text/plain',
    // json    application/json

    /* other
    bin     application/octet-stream
    bin     binary/octet-stream
    */
};

const mimeExt = {
    zip: ['7z', 'rar', 'tar', 'zip', 'gz', 'jar', 'bz', 'bz2'],
    img: ['ico', 'gif', 'png', 'jpg', 'jpeg', 'bmp', 'svg', 'webp'], //'avif', 'tiff',
    pdf,
    doc,
    xls,
    ppt,
    office: ([] as any[]).concat(pdf, doc, xls, ppt),
    audio: ['mp3', 'aac', 'wav', 'weba', 'audio-3gp', 'oga'],
    video: ['mp4', 'mov', 'avi', 'wmv', 'flv', 'mkv', 'mpg', 'mpeg', 'm4v', 'm4a', 'ogg', 'ogv', 'webm'],
};

export const mime = {
    ext: mimeExt,
    getExt(...types) {
        return ([] as any[])
            .concat(types)
            .flat()
            .map((t) => mimeExt[t] || t)
            .flat()
            .map((t) => `.` + t)
            .join(',');
    },

    type: extTypeMap,
    getType(name) {
        const last = this.getFileName(name);
        const ext = last.replace(/^.*\./, '');
        return extTypeMap[ext];
    },

    getFileName(name, lower = true) {
        const n = name.replace(/^.*[/\\]/, '');
        return lower ? n.toLowerCase() : n;
    },
};
